//////////////////////////////////////////////////////////////////////////////////////////////
//	File			:	"MainMenuState.cpp"
//
//	Author			:	Mike Davis, Matt Stover, James Thornton, Keith Webster
//	
//	Project			:	TacWars
//
//	Last Modified	:	11/6/2013
//
//	Purpose			:	
//
//////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "MainMenuState.h"
#include "GameplayState.h"
#include "OptionsState.h"
#include "InstructionsState.h"
#include "WorldMapState.h"
#include "GameLoadState.h"
#include "../../SGD Wrappers/CSGD_Direct3D.h"
#include "../../SGD Wrappers/CSGD_DirectInput.h"
#include "../../SGD Wrappers/CSGD_TextureManager.h"
#include "../../SGD Wrappers/CSGD_XAudio2.h"
#include "../../SGD Wrappers/CSGD_MessageSystem.h"
#include "../../SGD Wrappers/CSGD_EventSystem.h"
#include "../../SGD Wrappers/SGD_String.h"
#include "../../SGD Wrappers/SGD_Math.h"
#include "../MapSystem/BattleMap.h"
#include "../ParticleSystem/EmitterManager.h"
#include "../Game.h"
#include "../GameObjects/General.h"
#include "../GameObjects/Knight.h"
#include "../GameObjects/Ranger.h"
#include "../GameObjects/Priest.h"
#include "../GameObjects/Berserker.h"
#include <cassert>
#include "../GameObjects/Player.h"
#include "CreditsState.h"

MainMenuState* MainMenuState::GetInstance()
{
	static MainMenuState Instance;

	return &Instance;
}
MainMenuState::MainMenuState()
{
	cursor = 0;
}
MainMenuState::~MainMenuState()
{
}
/*virtual*/ void MainMenuState::Enter()
{
	EM = EmitterManager::GetInstance();
	EM->LoadEmitter("Cursor");
	animManager = CAnimationManager::GetInstance();
	aniInfo = new AnimInfo;
	const char* szFilename = "CommanderWalking.xml";
	if(animManager->LoadAnimation(szFilename) == false)
		return;

	aniInfo = animManager->GetAnimationInfo(0);
	cursorHorse = new Entity;
	cursorHorse->SetAnimInfo( aniInfo );
	aniInfo = animManager->GetAnimationInfo(0);
	cursorHorse->StartStopAnimation();
	animManager->PlayAnimation( aniInfo->currAnimation, *cursorHorse->GetAnimInfo(), cursorHorse );

	titleImgID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/MapStage3.png"));
	yUp = false;
	yDown = false;

}
/*virtual*/ void MainMenuState::Exit()
{
	for( int i = 0; i < animManager->GetAMSize(); i++)
		animManager->Unload( i );
}
/*virtual*/ bool MainMenuState::Input()
{
	CSGD_DirectInput* DI = CSGD_DirectInput::GetInstance();
	if(DI->JoystickIsUnplugged())
	{
		if(DI->KeyPressed( DIK_DOWN) == true)
		{
			++cursor;

			if(cursor > 5)
				cursor = 5;
		}
		else if(DI->KeyPressed(DIK_UP) == true)
		{
			--cursor;

			if(cursor < 0)
				cursor = 0;
		}

		if(DI->KeyPressed(DIK_RETURN) == true)
		{
			switch(cursor)
			{
			case 0:
				{
					Game::GetInstance()->PushState(GameLoadState::GetInstance());
				}
				break;
				//case 1:
				//	{
				//		//multiplayer
				//	}
				//	break;
			case 1:
				{
					Game::GetInstance()->PushState(GameLoadState::GetInstance());
				}
				break;
			case 2:
				{
					Game::GetInstance()->PushState(InstructionsState::GetInstance());
				}
				break;
			case 3:
				{
					Game::GetInstance()->PushState(OptionsState::GetInstance());
				}
				break;
			case 4:
				{
					Game::GetInstance()->PushState(CreditsState::GetInstance());
				}
				break;
			case 5:
				{
					return false;
				}
			}
		}

		if(DI->KeyPressed(DIK_ESCAPE) == true)
		{
			return false;
		}

	}
	else
	{
		Keyboard();
	}
	return true;
}
/*virtual*/ void MainMenuState::Update( float fElapsedTime )
{
	cursorHorse->SetPosX( (500 - (7*32)) / 2 );
	cursorHorse->SetPosY( 190 + (cursor * 50 ));
	animManager->UpdateAnimation( aniInfo->currAnimation, *cursorHorse->GetAnimInfo(), fElapsedTime);
	POINT emit;
	emit.x = (640 - (7*32)) / 2;
	emit.y = 232 + (cursor * 50);

	EM->StopEmitter("Cursor");
	EM->PlayEmitter(emit, nullptr, "Cursor");

	EM->UpdateAll(fElapsedTime);
}
/*virtual*/ void MainMenuState::Render()
{
	BitmapFont* Font = Game::GetInstance()->GetFont();
	CSGD_TextureManager::GetInstance()->Draw(titleImgID, 0, 0, 1.0f, 1.0f);

	Font->Draw( _T("TACWARS"), 
		(int)((640 - (3*24*2.0f)) / 2), 
		100, 2.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );

	

	Font->Draw( _T("Campaign"), (640 - (4*32)) / 2, 200, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );
	//Font->Draw( _T("MULTIPLAYER"), (640 - (4*32)) / 2, 250, 1.0f, D3DCOLOR_ARGB(255,255,255,255));
	Font->Draw( _T("LOAD GAME"), (640 - (4*32)) / 2, 250, 1.0f, D3DCOLOR_ARGB(255,255,255,255));
	Font->Draw( _T("INSTRUCTIONS"), (640 - (4*32)) / 2, 300, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );
	Font->Draw( _T("OPTIONS"), (640 - (4*32)) / 2, 350, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );
	Font->Draw( _T("CREDITS"), (640 - (4*32)) / 2, 400, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );
	Font->Draw( _T("QUIT"), (640 - (4*32)) / 2, 450, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );

	EM->RenderAll();
	Font->Draw( _T("(      \t\t\t\t   )"), (640 - (7*32)) / 2, 200 + (cursor * 50), 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );

	animManager->RenderAnimation( aniInfo->currAnimation, *cursorHorse->GetAnimInfo(), cursorHorse );

}
bool MainMenuState::Keyboard()
{
	CSGD_DirectInput* DI = CSGD_DirectInput::GetInstance();

	if(DI->JoystickGetLStickYNormalized() > 0 && yDown == false)
	{
		++cursor;

		if(cursor > 5)
			cursor = 5;
		yDown = true;
	}
	else if(DI->JoystickGetLStickYNormalized() < 0 && yUp == false)
	{
		--cursor;

		if(cursor < 0)
			cursor = 0;
		yUp = true;
	}

	if(DI->JoystickButtonPressed(0) == true)
	{
		switch(cursor)
		{
		case 0:
			{
				Game::GetInstance()->PushState(WorldMapState::GetInstance());
			}
			break;
			//case 1:
			//	{
			//		//multiplayer
			//	}
			//	break;
		case 1:
			{
				Game::GetInstance()->PushState(GameLoadState::GetInstance());
			}
			break;
		case 2:
			{
				Game::GetInstance()->PushState(InstructionsState::GetInstance());
			}
			break;
		case 3:
			{
				Game::GetInstance()->PushState(OptionsState::GetInstance());
			}
			break;
		case 4:
			{
				Game::GetInstance()->PushState(CreditsState::GetInstance());
			}
			break;
		case 5:
			{
				return false;
			}
		}
	}

	if(DI->JoystickButtonPressed(1) == true)
	{
		return false;
	}
	if(DI->JoystickGetLStickYNormalized() == 0)
	{
		yUp = false;
		yDown = false;
	}
	return true;
}